package com.lyl.xxx.common.config;

import cn.hutool.core.thread.NamedThreadFactory;
import cn.hutool.core.util.RuntimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Mr.Luo
 * @date 2022-10-14 11:11
 * @desc 线程池配置
 */
@Slf4j
@Configuration
public class ThreadPoolConfig {
    public static final int CPU = RuntimeUtil.getProcessorCount();

    @Bean
    public Executor logExecutor() {
        return new ThreadPoolExecutor(
                CPU,
                2 * CPU + 1,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                new NamedThreadFactory("log-pool-", false),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
}
